package net.yegong.util;

/**
 * 
 * The Pair class can contains two elements with different type.
 * @author cooper
 */
public final class Pair<U, V> {

	public Pair(U first, V second) {
		this.first = first;
		this.second = second;
	}

	public U first() {
		return first;
	}

	public V second() {
		return second;
	}

	public static <U, V> Pair<U, V> pair(U u, V v) {
		return new Pair<U, V>(u, v);
	}

	@Override
	public String toString() {
		return "[" + first + ", " + second + "]";
	}

	@Override
	public int hashCode() {
		int hash1 = 0;
		int hash2 = 0;
		if (first != null) {
			hash1 = first.hashCode();
		}
		if (second != null) {
			hash2 = second.hashCode();
		}
		return (hash1 * 31) ^ hash2;
	}

	@Override
	public boolean equals(Object o) {
		if (o == null) {
			return false;
		}
		if (o instanceof Pair<?, ?>) {
			Pair<?, ?> p = (Pair<?, ?>) o;
			boolean eq1 = (first == p.first) || (first != null && first.equals(p.first)); 
			boolean eq2 = (second == p.second) || (second != null && second.equals(p.second)); 
			return eq1 && eq2;
		}
		return false;
	}

	private U first;
	private V second;
}
